<?php

/**
 * This is the model class for table "{{audios}}".
 *
 * The followings are the available columns in table '{{audios}}':
 * @property string $audio_id
 * @property string $audio_name
 * @property string $audio_saltid
 * @property string $audio_tags
 * @property string $audio_cover
 * @property string $audio_file
 * @property integer $audio_size
 * @property integer $audio_length
 * @property integer $audio_from
 * @property integer $audio_playtimes
 * @property integer $audio_likes
 * @property string $audio_status
 * @property integer $audio_dateline
 */
class Audios extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{audios}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('audio_size, audio_length, audio_from, audio_playtimes, audio_likes, audio_dateline', 'numerical', 'integerOnly'=>true),
			array('audio_file', 'file','types'=>'mp3,f4v'),
			array('audio_cover', 'file','types'=>'jpg,gif,png,jpeg'),
			array('audio_status', 'length', 'max'=>1),
			array('audio_tags', 'normalizeTags'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('audio_id, audio_name, audio_saltid, audio_tags , audio_shares , audio_cover, audio_file, audio_size, audio_length, audio_from, audio_playtimes, audio_likes, audio_status, audio_dateline', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				
		);
	}
	
	/**
	 * Normalizes the user-entered tags.
	 */
	public function normalizeTags($attribute,$params)
	{
		$this->audio_tags=AudioTags::array2string(array_unique(AudioTags::string2array($this->audio_tags)));
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'audio_id' => '音频ID',
			'audio_name' => '音频名称',
			'audio_file' => '音频文件的地址',
			'audio_size' => '音频的大小',
			'audio_cover' => '音频封面',
			'audio_saltid'=>'',
			'audio_length' => '音频的长度 3m2s',
			'audio_shares'=>'音频分享的次数',
			'audio_from' => '音频的来源',
			'audio_tags' =>'标签',
			'audio_playtimes' => '音频播放次数',
			'audio_likes' => '音频被赞的次数',
			'audio_status' => '1 审核通过 0 违法内容屏蔽',
			'audio_dateline' => '音频上传的时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('audio_id',$this->audio_id,true);
		$criteria->compare('audio_name',$this->audio_name,true);
		$criteria->compare('audio_from',$this->audio_from);
		$criteria->compare('audio_playtimes',$this->audio_playtimes);
		$criteria->compare('audio_likes',$this->audio_likes);
		$criteria->compare('audio_status',$this->audio_status,true);
		$criteria->compare('audio_dateline',$this->audio_dateline);
		
		$criteria -> order = "audio_dateline desc";

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 记录访问的记录
	 * @param $audio_model unknown_type
	 */
	public function setHistory($audio_model) {
	
		$uid = Yii::app ()->user->id;
	
		$audio_history = AudioHistory::model ()->findByAttributes ( array ('audio_id' => $audio_model->audio_id ) );
	
		if ($audio_history) {
			$audio_history->dateline = time ();
			$audio_history->update ();
			return $audio_history;
		} else {
			$audio_history = new AudioHistory ();
			$audio_history->history_id = NULL;
			$audio_history->history_from = $uid;
			$audio_history->audio_id = $audio_model->audio_id;
			$audio_history->dateline = time ();
			$audio_history->starttime = 0;
			$audio_history->save ();
			return $audio_history;
		}
	
	}
	
	/**
	 * 短地址
	 * @param string $url
	 */
	public function urlShort($url){
		$url=crc32($url);
		$x=sprintf('%u',$url);
	
		$show='';
		while($x>0){
			$s=$x%62;
			if($s>35){
				$s=chr($s+61);
			}elseif($s>9&&$s<=35){
				$s=chr($s+55);
			}
			$show.=$s;
			$x=floor($x/62);
		}
		return $show;
	}
	
	public function beforeSave(){
		$this -> audio_saltid = $this -> urlShort($this -> audio_file);
		return true;
	}
	
	public function afterSave(){
		parent::afterSave();
		AudioTags::model()->updateFrequency('',$this -> audio_tags);
	}
	
	
	/**
	 * This is invoked after the record is deleted.
	 */
	protected function afterDelete()
	{
		parent::afterDelete();
		AudioTags::model()->updateFrequency($this->tags, '');
	}
	
	public function getAudio($id){
		return $this -> findByPk($id);
	}
	
	/**
	 * 
	 */
	public function updateSalt(){
		$datas = $this -> findAll();
		foreach($datas as $data){
			$data -> audio_saltid = $this -> urlShort($data -> audio_file);
			$data -> update();
		}
	}
	
	/**
	 * 返回音频访问URL
	 */
	public function getAccessUrl(){
		return Yii::app()-> createUrl('/shuo/'.$this -> audio_saltid);
	}
	
	/**
	 * 显示音频的封面
	 */
	public function getAccessCover(){
		return Yii::app() -> qiniu -> getAudioCover($this -> audio_cover);
	}
	
	/**
	 * 显示音频地址
	 */
	public function getAccessAudio(){
		return Yii::app() -> qiniu -> getAudio($this -> audio_file);
	}
	
	/**
	 * 赞的地址
	 */
	public function getAudioLikeUrl(){
		return Yii::app() -> createUrl(array ('/play/audio/like' ));
	}
	
	/**
	 * 取消赞的地址
	 */
	public function getAudioDislikeUrl(){
		return Yii::app() -> createUrl(array ('/play/audio/like' ));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Audios the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	/**
	 *
	 * @param $eduid int
	 */
	public function getAudioByEduer($eduid) {
		$criteria = new CDbCriteria ();
		$criteria->compare ( 'audio_from', $eduid );
		return $this->findAll ( $criteria );
	}
}
